import router from '@/router'
import userCookies from "@/utils/data/cookies";
import { info, pwd } from "@/api/core/user";
export const actions = {
    //  修改密码
    pwd(context: any, params: any) {
        return pwd(params)
    },
    /**
     * @description 清空所有数据
     * @param {Object} context
     */
    async databaseClear({
        dispatch
    }: any) {
        // 清空公用数据
        dispatch('coreDb/databaseClear', {}, {
            root: true
        })
        // 清空用户数据
        return dispatch('dbSet');
    },
    /**
     * @description 存储用户数据
     * @description 效果类似于取值 dbName.path = value
     * @param {Object} context
     * @param {Object} payload dbName {String} 数据库名称
     * @param {Object} payload path {String} 存储路径
     * @param {Object} payload value {*} 需要存储的值
     * @param {Object} payload user {Boolean} 是否区分用户
     */
    async dbSet({
        dispatch
    }: any, {
        dbName = "sys",
        path = "",
        value = '',
        user = true
    }: any = {}) {
        return dispatch('coreDb/set', {
            dbName,
            path,
            value,
            user
        }, {
            root: true
        })
    },
    /**
     * @description 效果类似于取值 dbName.path || defaultValue
     * @param {Object} context
     * @param {Object} payload dbName {String} 数据库名称
     * @param {Object} payload path {String} 存储路径
     * @param {Object} payload defaultValue {*} 取值失败的默认值
     * @param {Object} payload user {Boolean} 是否区分用户
     */
    async dbGet({
        dispatch
    }: any, {
        dbName = "sys",
        path = "",
        value = '',
        user = true
    }: any = {}) {
        return dispatch('coreDb/get', {
            dbName,
            path,
            value,
            user
        }, {
            root: true
        })
    },
    /**
     * @description 从数据库取用户数据
     * @param {Object} context
     */
     async load({
        state, dispatch
    }: any) {
        return new Promise((resolve) => {
            dispatch('dbGet', {
                path: 'info'
            }).then((data: any) => {
                state.info = data;
                // console.log(data)
                resolve(data);
            });
        });
    },
    /**
     * @description 登录成功
     * @param {Object} context
     */
     async loginSuccess({ dispatch }: any, data: any = {}) {
        // 存储用户信息
        dispatch('dbSet', {
            path: 'info',
            value: data
        })
        userCookies.set(data.token)
        // 跳转到首页 index
        if (data.user_type == 2) {
            router.push({
                name: 'parkManagement'
            });
        } else {
            router.push({
                name: 'parkTable'
            });
        }

    },
    /**
     * @description 注销用户并返回登录页面
     * @param {Object} context
     */
    async logout({
        dispatch
    }: any = {}) {
        // await logout();
        // 不管成功失败都需要清除数据，避免死循环
        dispatch('coreDb/databaseClear', {}, {
            root: true
        });
        dispatch('corePage/closeAll', {}, {
            root: true
        });
        dispatch('logoutTodo');
    },
    // 退出登录，清空数据
    logoutTodo() {
        // 清空登录凭证
        userCookies.remove();
        // 重定向到登录页面,这样写是为了清除掉url中get参数
        location.href = `${location.origin}${location.pathname}#login`;
        // 直接刷新页面，重置所有缓存数据
        location.reload();
    }
}
